﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RepositoryBLL = Dao.Repository;
using UQBuyBLL = Dao.UQBuy;
using Domain.Model.Entity;
using Domain.UQBuy.Model.Entity;
using System.IO;


namespace Client.UQBuy.Web.Areas.Manage.Controllers
{
    public class UserController : Controller
    {
        // GET: /Manage/User/
        public RepositoryBLL.IUserDao UserBll { get; set; }
        public RepositoryBLL.IUserDetailDao UserDetailBll { get; set; }
        public UQBuyBLL.IUserDao UserBllUQBuy { get; set; }
        public string Message { get; set; }
        public static readonly log4net.ILog custom = log4net.LogManager.GetLogger("Custom");
        public static readonly log4net.ILog common = log4net.LogManager.GetLogger("Common");
        public ActionResult Index()
        {

            return View();
        }

        //public ActionResult SetUser(string id)
        //{
        //    OZ_User user = UserBllUQBuy.Get(id);
        //    return View(user);
        //}


        public ActionResult UserList()
        {
            OZ_User user = new OZ_User() { User_ID = Guid.NewGuid().ToString(), User_Code = "test", User_CreateDate = DateTime.Now, User_Email = "yexinw@163.com", User_LoginName = "yexinw", User_Mobile = "18721982886", User_Password = "123456", User_RealName = "王业鑫", User_Status = 1 };
            UserBllUQBuy.Save(user);



            custom.Info(Message);
            common.Debug(Message);
            List<OZ_User> listUser = new List<OZ_User>();
            // listUser = UserBllUQBuy.GetPageList(4, 10).ToList<OZ_User>();

            //listUser = UserBllUQBuy.FindPageList(typeof(OZ_User).GetProperty("user_CreateDate"), "ASC", 5, 5).ToList<OZ_User>();
            listUser = UserBllUQBuy.GetAll().ToList();
            ViewBag.Message = Message;
            return View(listUser);
        }

        public ActionResult UserList2()
        {
            OZUser user = new OZUser() { UserID = Guid.NewGuid().ToString(), UserCode = "test", UserEmail = "yexinw@163.com", UserLoginName = "yexinw", UserMobile = "18721982886", UserPassword = "123456", UserRealName = "王业鑫", UserStatus = 1 };
            UserBll.Save(user);
            OZUserDetail userDetail = new OZUserDetail() { OZUser = user, DetailAge = 21, DetailID = Guid.NewGuid().ToString() };
            UserDetailBll.Save(userDetail);
            custom.Info(Message);
            common.Debug(Message);
            List<OZUser> listUser = new List<OZUser>();
            List<OZUserDetail> listUserDetail = new List<OZUserDetail>();
            listUser = UserBll.GetPageList(4, 10).ToList<OZUser>();

            //listUser = UserBll.FindPageList(typeof(OZUser).GetProperty("UserCreateDate"), "ASC", 5, 5).ToList<OZUser>();
            //listUser = UserBll.GetAll().ToList();
            ViewBag.listUserDetail = UserDetailBll.GetAll().ToList<OZUserDetail>();

            //OZUser user1 = UserBll.Get("01ebf92b-04a5-447e-bf62-ee40782bfe12");
            // OZUserDetail ozud = user1.OZUserDetail[0];

            //listUserDetail.Add(UserDetailBll.Get("d96b5e2d-7099-45b9-b180-8b644ef9a25c"));
            //ViewBag.listUserDetail = listUserDetail;
            //listUser.Add(UserDetailBll.Get("d96b5e2d-7099-45b9-b180-8b644ef9a25c").OZUser);


            ViewBag.Message = Message;
            return View(listUser);
        }

        public ActionResult East()
        {
            ViewBag.test = "wangyexin";
            return View("~/Areas/Manage/Views/Shared/Layout/ViewEast.cshtml");
        }
        public ActionResult North()
        {
            return View();
        }
        public ActionResult South()
        {
            return View();
        }
        public ActionResult West()
        {
            return View();
        }

        public ActionResult test()
        {
            OZUser user = UserBll.Get("ed681e41-1cd5-4b32-a8e8-17392541e964");
            List<OZUserDetail> listUserDetail = user.OZUserDetail.ToList<OZUserDetail>();


            return View();
        }

        public ActionResult test2()
        {
            return View();
        }

        public ActionResult test3()
        {
            OZUser user = UserBll.Get("14e2844e-58fe-45ec-abd1-119e996581b5");
            List<OZUserDetail> listUserDetail = user.OZUserDetail.ToList<OZUserDetail>();
            ViewBag.user1 = listUserDetail[0];
            return View(listUserDetail);
        }

        public ActionResult Upload()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Upload(HttpPostedFileBase fileData)
        {
            if (fileData != null)
            {
                try
                {
                    // 文件上传后的保存路径
                    string filePath = Server.MapPath("~/Uploads/");
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    string fileName = Path.GetFileName(fileData.FileName);// 原始文件名称
                    string fileExtension = Path.GetExtension(fileName); // 文件扩展名
                    string saveName = Guid.NewGuid().ToString() + fileExtension; // 保存文件名称

                    fileData.SaveAs(filePath + saveName);

                    return Json(new { Success = true, FileName = fileName, SaveName = saveName });
                }
                catch (Exception ex)
                {
                    return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(new { Success = false, Message = "请选择要上传的文件！" }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
